package com.kj.tms.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ProjectName: tms
 * @Package: com.hongtu.tms.common.util
 * @ClassName: ${NumberUtils}
 * @Description: [数字转换及判断相关工具类]
 * @Author: [zhuyongjie]
 * @CreateDate: 2017/11/1 10:14
 * @UpdateUser: []
 * @UpdateDate: 2017/11/1 10:14
 * @UpdateRemark: []
 * @Version: 1.0
 * Copyright: 北京宏图天安 Copyright (c) 2017
 */
public class NumberUtils {
    /**
     * 正则匹配
     *
     * @param regex   正则表达式
     * @param orginal 需判断字符串
     * @return
     */
    private static boolean isMatch(String regex, String orginal) {
        if (orginal == null || orginal.trim().equals("")) {
            return false;
        }
        Pattern pattern = Pattern.compile(regex);
        Matcher isNum = pattern.matcher(orginal);
        return isNum.matches();
    }

    /**
     * 是否正整数
     *
     * @param orginal
     * @return
     */
    public static boolean isPositiveInteger(String orginal) {
        return isMatch("^\\+{0,1}[1-9]\\d*", orginal);
    }

    /**
     * 是否负整数
     *
     * @param orginal
     * @return
     */
    public static boolean isNegativeInteger(String orginal) {
        return isMatch("^-[1-9]\\d*", orginal);
    }

    /**
     * 是否整数
     *
     * @param orginal
     * @return
     */
    public static boolean isWholeNumber(String orginal) {
        return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal)
                || isNegativeInteger(orginal);
    }

    /**
     * 是否正小数
     *
     * @param orginal
     * @return
     */
    public static boolean isPositiveDecimal(String orginal) {
        return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal);
    }

    /**
     * 是否负小数
     *
     * @param orginal
     * @return
     */
    public static boolean isNegativeDecimal(String orginal) {
        return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*", orginal);
    }

    /**
     * 是否小数
     *
     * @param orginal
     * @return
     */
    public static boolean isDecimal(String orginal) {
        return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal);
    }

    /**
     * 是否数字
     *
     * @param orginal
     * @return
     */
    public static boolean isRealNumber(String orginal) {
        return isWholeNumber(orginal) || isDecimal(orginal);
    }

    public static void main(String[] args) {
        String str = "123";
        String str2 = "123.00";
        System.out.println("是否整数str：" + isWholeNumber(str));
        System.out.println("是否整数str2：" + isWholeNumber(str2));
        System.out.println("是否小数str:" + isDecimal(str));
        System.out.println("是否小数str2:" + isDecimal(str2));
        System.out.println(str.format("%.2f", 123.22323f));
    }
}
